<?php
namespace app\index\controller;

use think\Controller;
use think\Db;

class Index extends Controller
{
    public function index()
    {
        $list = Db::name('user')->select();
        return view('index/index',[
            'list' => $list
        ]);
    }

    public function del()
    {
        $id = input('id');
        $res = Db::name('user')->where('id',$id)->delete();
        if ($res > 0) {
            return $this->success('删除成功', url('index/index/index'));
        } else {
            return $this->error('删除失败,请重试');
        }
    }

    public function add()
    {
        $data = input('post.');
        $res = Db::name('user')->insert($data);
        if ($res > 0) {
            return $this->success('添加成功', url('index/index/index'));
        } else {
            return $this->error('添加失败,请重试');
        }
    }

    public function cha()
    {
        $id = input('id');
        $res = Db::name('user')->where('id',$id)->find();
        if ($res > 0) {
            return view('index/edit',[
                'res' => $res
            ]);
        } else {
            return $this->error('查询失败,请重试');
        }
    }

    public function edit()
    {
        $data = input('post.');
        $id =  $data['id'];
        $res = Db::name('user')->where('id',$id)->update($data);
        if ($res > 0) {
            return $this->success('编辑成功', url('index/index/index'));
        } else {
            return $this->error('编辑失败,请重试');
        }
    }
}
